package xj.graph2d.app.prop;

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.File;
import java.io.IOException;

import javax.swing.ImageIcon;
import javax.swing.JComponent;
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileFilter;

import xj.app.DrawToolkit;
import xj.graph2d.DrawCanvas;
import xj.graph2d.DrawShape;
import xj.graph2d.DrawShapeConstants;
import xj.graph2d.DrawShapeListener;
import xj.graph2d.SimpleDrawDocument;
import xj.graph2d.SimpleDrawView;
import xj.graph2d.app.DrawAction;
import xj.graph2d.shapes.ImageShape;
import xj.graph2d.tools.OnePointShapeTool;
import xj.util.gui.MultiExtensionsFileFilter;

public class ImageShapeListener 
  extends xj.graph2d.app.DrawAction 
  implements ActionListener, DrawShapeListener {

  public ImageShapeListener() {
    FileFilter imageFileFilter = new MultiExtensionsFileFilter(extensions,
							       "Raster image files (.jpg .bmp .gif .png)");
    chooser = new JFileChooser();
    chooser.addChoosableFileFilter(imageFileFilter);
    // chooser.setCurrentDirectory(new File(DrawApp.defaultDir));
    previewer = new FilePreviewer(chooser);
    chooser.setAccessory(previewer);
  }

  public void setCurrentDirectory(String dir) {
    if (dir != null) {
      chooser.setCurrentDirectory(new File(dir));
    }
  }

  public void actionPerformed(ActionEvent e) {
    previewer.loadImage(null);
    chooser.rescanCurrentDirectory();
    int retval = chooser.showOpenDialog(null);
    if (retval == JFileChooser.APPROVE_OPTION) {
      File theFile = chooser.getSelectedFile();
      if (theFile != null) {
	if (theFile.isFile()) {
	  String filename = chooser.getSelectedFile().getAbsolutePath();
	  try {
	    ImageShape ishape = new ImageShape(filename, true);
	    SimpleDrawView view = main.getView();
	    SimpleDrawDocument doc = main.getDoc();
	    DrawToolkit toolkit = main.getDrawToolkit();
	    OnePointShapeTool tool = (OnePointShapeTool) toolkit.getTool("ImageTool");
	    if (tool == null) { 
	      tool = new OnePointShapeTool(ishape,
					   DrawShapeConstants.CLONE_PROTOTYPE_DRAW_ATTR);
	      main.addTool("ImageTool", tool);
	    } else { 
	      tool.setPrototype(ishape);
	    }
	    view.setCurrentTool(tool);
	    DrawCanvas canvas = view.getCanvas();
	    Point p = canvas.getMousePosition();
	    tool.mouseMoved(view, p, 0);
	  } catch (IOException ex) {
	    System.out.println("Error: reading image file " + ex.getMessage());
	  }
	}
      }
    }
  }

  public void notify(DrawShape shape) {
    if (shape instanceof ImageShape) {
      ImageShape ishape = (ImageShape) shape;

      previewer.loadImage(null);
      chooser.rescanCurrentDirectory();
      int retval = chooser.showOpenDialog(null);
      if (retval == JFileChooser.APPROVE_OPTION) {
	File theFile = chooser.getSelectedFile();
	if (theFile != null) {
	  if (theFile.isFile()) {
	    String filename = chooser.getSelectedFile().getAbsolutePath();
	    try {
	      ishape.setImage(filename);
	    } catch (IOException e) {
	      System.out.println("Error: reading image file " + e.getMessage());
	    }
	  }
	}
      }

    }
  }

  protected String[] extensions = { "jpeg", "bmp", "gif", "png", "jpg" };

  protected JFileChooser chooser;

  protected FilePreviewer previewer;

  class FilePreviewer extends JComponent implements PropertyChangeListener {
    ImageIcon thumbnail = null;

    public FilePreviewer(JFileChooser fc) {
      setPreferredSize(new Dimension(100, 100));
      fc.addPropertyChangeListener(this);
    }

    public void loadImage(File f) {
      if (f == null) {
	thumbnail = null;
      } else {
	ImageIcon tmpIcon = new ImageIcon(f.getPath());
	if (tmpIcon.getIconWidth() > 90) {
	  thumbnail = new ImageIcon(tmpIcon.getImage().getScaledInstance(90, -1, Image.SCALE_DEFAULT));
	} else {
	  thumbnail = tmpIcon;
	}
      }
    }

    public void propertyChange(PropertyChangeEvent e) {
      String prop = e.getPropertyName();
      if (prop == JFileChooser.SELECTED_FILE_CHANGED_PROPERTY) {
	if (isShowing()) {
	  loadImage((File) e.getNewValue());
	  repaint();
	}
      }
    }

    @Override
    public void paint(Graphics g) {
      if (thumbnail != null) {
	int x = getWidth() / 2 - thumbnail.getIconWidth() / 2;
	int y = getHeight() / 2 - thumbnail.getIconHeight() / 2;
	if (y < 0) {
	  y = 0;
	}

	if (x < 5) {
	  x = 5;
	}
	thumbnail.paintIcon(this, g, x, y);
      }
    }
  }

}
